﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XmlServer.ServiceContracts;
using XmlServer.ServiceImplementation;


namespace ExceptionOperator
{
    public class Operator : IOperator
    {

        public const string SERVER_NAME = "Exception Operator";
        public const string SERVER_VERSION = "1.0";

        public string Name
        {
            get { return SERVER_NAME; }
        }

        public string Version
        {
            get { return SERVER_VERSION; }
        }

        public string FullName
        {
            get { return String.Format("{0}_{1}", SERVER_NAME, Version); }
        }

        public string Description
        {
            get { return "Simple operator that thows an exception"; }
        }

        public List<IFact> Facts
        {
            get 
            {
                return new List<IFact>
                {
                    new Fact { Name = "ThrowException", IsRequired = true, Description = "True/False indicating is we should throw a .Net Exception or place the exception in the response facts" }
                };
            }
        }

        public IResponse ProcessRequest(IRequest request)
        {
            Response response = new Response();
            
            // get the "Facts" we need from the request
            var throwException  = XmlServer.Helper.Utilities.GetFact<bool>(request.Facts, "ThrowException");
            if (throwException)
            {
                // throw a .Net exception in the operator
                throw new Exception("This is a test Exception from the client operator");
            }

            // gracefully return an exception in response object
            response.Facts = XmlServer.Helper.Utilities.CreateException("This is a test Exception from the client operator");
            response.Success = false;
            response.StopProcessing = System.Environment.TickCount;
            return response;

        }


    }
}
